package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.AdminPreidCourse;
import com.ruoyi.system.domain.AdminVideo;
import com.ruoyi.system.service.IAdminPreidCourseService;
import com.ruoyi.system.service.IAdminVideoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 视频Controller
 * 
 * @author ruoyi
 * @date 2020-06-17
 */
@Controller
@RequestMapping("/system/video")
public class AdminVideoController extends BaseController
{
    private String prefix = "system/video";

    @Autowired
    private IAdminVideoService adminVideoService;
    @Autowired
    private IAdminPreidCourseService adminPreidCourseService;

    @RequiresPermissions("system:video:view")
    @GetMapping()
    public String video()
    {
        return prefix + "/video";
    }

    /**
     * 查询视频列表
     */
    @RequiresPermissions("system:video:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AdminVideo adminVideo)
    {
        startPage();
        List<AdminVideo> list = adminVideoService.selectAdminVideoList(adminVideo);
        return getDataTable(list);
    }

    /**
     * 导出视频列表
     */
    @RequiresPermissions("system:video:export")
    @Log(title = "视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AdminVideo adminVideo)
    {
        List<AdminVideo> list = adminVideoService.selectAdminVideoList(adminVideo);
        ExcelUtil<AdminVideo> util = new ExcelUtil<AdminVideo>(AdminVideo.class);
        return util.exportExcel(list, "video");
    }

    /**
     * 新增视频
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存视频
     */
    @RequiresPermissions("system:video:add")
    @Log(title = "视频", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AdminVideo adminVideo)
    {
    	adminVideo.setCompanyId(ShiroUtils.getCompanyId());
    	adminVideo.setCompanyName(ShiroUtils.getCompanyName());
    	adminVideo.setUserId(ShiroUtils.getUserId());
    	adminVideo.setCreateBy(ShiroUtils.getSysUser().getUserName());
        return toAjax(adminVideoService.insertAdminVideo(adminVideo));
    }

    /**
     * 修改视频
     */
    @GetMapping("/edit/{videoId}")
    public String edit(@PathVariable("videoId") Long videoId, ModelMap mmap)
    {
        AdminVideo adminVideo = adminVideoService.selectAdminVideoById(videoId);
        mmap.put("adminVideo", adminVideo);
        return prefix + "/edit";
    }

    /**
     * 修改保存视频
     */
    @RequiresPermissions("system:video:edit")
    @Log(title = "视频", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AdminVideo adminVideo)
    {
    	adminVideo.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(adminVideoService.updateAdminVideo(adminVideo));
    }

    /**
     * 删除视频
     */
    @RequiresPermissions("system:video:remove")
    @Log(title = "视频", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(adminVideoService.deleteAdminVideoByIds(ids));
    }
    
    /**
     * 视频状态修改
     */
    @Log(title = "视频管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:video:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(AdminVideo adminVideo)

    {
        return toAjax(adminVideoService.changeStatus(adminVideo));
    }
    /**
     * 我的课程- 添加视频-视频列表
     * @param preidId
     * @param mmap
     * @return
     */
    @RequiresPermissions("system:theory:view")
    @GetMapping("/{preidId}")
    public String video2(@PathVariable("preidId") Long preidId, ModelMap mmap)
    {
      mmap.put("preidId", preidId);
      return prefix + "/video2";
    }


    /**
     * 新增保存曲目
     */
    @RequiresPermissions("system:video:add")
    @Log(title = "曲目", businessType = BusinessType.INSERT)
    @PostMapping("/add2")
    @ResponseBody
    public AjaxResult addSave2(AdminVideo adminVideo)
    {

      AdminPreidCourse adminPreidCourse = new AdminPreidCourse ();
      adminPreidCourse.setCourseId(adminVideo.getVideoId());
      adminPreidCourse.setCourseName(adminVideo.getVideoName());
      adminPreidCourse.setCourseType("5");
      adminPreidCourse.setPreidId(adminVideo.getPreidId());
      adminPreidCourse.setCreateBy(ShiroUtils.getSysUser().getUserName());


      return toAjax(adminPreidCourseService.insertAdminPreidCourse(adminPreidCourse));
    }



    /**
     * 查询曲目列表
     */
    @RequiresPermissions("system:video:list")
    @PostMapping("/list2")
    @ResponseBody
    public TableDataInfo list2(AdminVideo adminVideo)
    {
      startPage();
      List<AdminVideo> list = adminVideoService.selectAdminVideoList(adminVideo);
      AdminPreidCourse adminPreidCourse = new AdminPreidCourse();
      for (AdminVideo video : list) {
        adminPreidCourse.setCourseId(video.getVideoId());
        adminPreidCourse.setPreidId(adminVideo.getPreidId());
        adminPreidCourse.setCourseType("5");
        adminPreidCourse.setCreateBy(ShiroUtils.getSysUser().getUserName());
        List<AdminPreidCourse> adminPreidCourses = adminPreidCourseService.selectAdminPreidCourseList(adminPreidCourse);
        if(null!=adminPreidCourse && adminPreidCourses.size()>0){
          video.setAddPeriedFlag("1");
        }

      }
      return getDataTable(list);
    }

    /**
     * 删除课时曲目
     */
    @RequiresPermissions("system:video:remove")
    @Log(title = "曲目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove2")
    @ResponseBody
    public AjaxResult remove2(AdminVideo adminVideo)
    {
      AdminPreidCourse adminPreidCourse = new AdminPreidCourse ();
      adminPreidCourse.setCourseId(adminVideo.getVideoId());
      adminPreidCourse.setPreidId(adminVideo.getPreidId());
      adminPreidCourse.setCreateBy(ShiroUtils.getSysUser().getUserName());


      return toAjax(adminPreidCourseService.deleteAdminPreidCourse(adminPreidCourse));
    }

}
